package LC_101_200;

import java.util.HashSet;

/**
 * https://leetcode-cn.com/problems/longest-consecutive-sequence/solution/hashsetyi-dan-yi-ge-shu-bei-sao-miao-zhi-jie-jiang/
 * 思路：
 * 对于数组中的每一个数，在hashSet中找比它小的，和比它大的，并进行计数。
 */
public class LC128 {
    public int longestConsecutive(int[] nums) {
        HashSet<Integer> set = new HashSet<>();
        for (int n : nums) {
            set.add(n);
        }
        int temp1 = 0;
        int temp2 = 0;
        int count = 0;
        int res = 0;
        for (int num : nums) {
            if (!set.contains(num)) {
                continue;
            }
            count = 0;
            temp1 = num;
            temp2 = num + 1;
            while (set.contains(temp1)) {
                count++;
                set.remove(temp1);
                temp1--;
            }
            while (set.contains(temp2)) {
                count++;
                set.remove(temp2);
                temp2++;
            }
            res = Math.max(res, count);
        }
        return res;
    }
}
